<template>
  <div>
    <el-card>
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" @keyup.enter.native="getGoodsList" :clearable="true"
                    @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <router-link to="/goods/add">
            <el-button type="primary">添加商品</el-button>
          </router-link>
        </el-col>
      </el-row>

      <el-table v-loading="loading" border :data="tableData" style="width: 100%">
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="125px" align="center"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="125px" align="center"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150px" align="center">
          <template v-slot="scopeInfo">
            {{ scopeInfo.row.add_time | format }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140px" align="center">
          <template v-slot="scopeInfo">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { apiGetGoodsList } from '@/api/index.js'

export default {
  name: 'List',
  created () {
    this.getGoodsList()
  },
  data: function () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  methods: {
    // 获取数据
    async getGoodsList () {
      this.loading = true
      const { data: res } = await apiGetGoodsList(this.queryInfo)
      this.loading = false
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.status + ' ' + res.meta.msg)
      }
      this.tableData = res.data.goods
      this.total = res.data.total
    },
    // 页面展示数大小发生改变
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    // 改变当前页数
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    }
    // TODO 商品列表 编辑，删除功能
  }
}
</script>

<style scoped>

</style>
